package com.darrenchan.leetcode;

import java.util.Arrays;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-02-22 19:23
 */
public class QLint61 {
    public int[] searchRange(int[] A, int target) {
        int[] res = new int[2];
        Arrays.fill(res, -1);
        if(A == null || A.length == 0) {
            return res;
        }

        int start = 0, end = A.length - 1;
        while (start + 1 < end) {
            int mid = start + (end - start) / 2;
            if(target > A[mid]) {
                start = mid;
            } else {
                end = mid;
            }
        }

        if(A[start] == target) {
            res[0] = start;
        } else if(A[end] == target) {
            res[0] = end;
        }

        start = 0;
        end = A.length - 1;
        while (start + 1 < end) {
            int mid = start + (end - start) / 2;
            if(target < A[mid]) {
                end = mid;
            } else {
                start = mid;
            }
        }

        if(A[end] == target) {
            res[1] = end;
        } else if(A[start] == target) {
            res[1] = start;
        }

        return res;
    }
}
